// 当前用户id
// console.log(userInfo._id);
let collectArr;
let currentCollectPage = 0;
//获取所有试题
function getAllCollect() {
  $.ajax({
    url: "/collections/getCollections",
    type: "post",
    data: { studentId: userInfo._id },
    success(res) {
      collectArr = res.result || [];
    }
  });
}
getAllCollect();

// console.log(collectArr);

// 渲染收藏题目
renderCollect();
function renderCollect() {
  getAllCollect();

  // 渲染错题总数量
  $("#colNum").text(collectArr.length);
  // 数据库没有数据直接返回
  if (collectArr.length == 0) return;

  //渲染错题到页面
  collectArr.forEach(forCallCollect);

  // 让第一个显示
  $(`.colectChange0`).show();

  //删除收藏
  $(".deleteCollect").on("click", function () {
    let _id = $(this).data("deletecollect");
    $.ajax({
      url: "/collections/deleteCollections",
      type: "post",
      data: { _id },
      success(res) {
        console.log(res);
        $("#collectContent").html("");
        renderCollect();
      }
    });
  });

  // 上一题
  $(".preCol").on("click", function () {
    if (currentCollectPage <= 0) return alert("前面没有了");
    currentCollectPage--;
    $(`.colectChange${currentCollectPage}`).show().siblings().hide();
    // console.log(currentCollectPage);
  });

  // 下一题
  $(".nextCol").on("click", function () {
    if (currentCollectPage >= collectArr.length - 1) return alert("已经到底了");
    currentCollectPage++;
    $(`.colectChange${currentCollectPage}`).show().siblings().hide();
    // console.log(currentCollectPage);
  });
}

//渲染收藏的foreach回调函数
function forCallCollect(v, index) {
  let exeInfo = v.exerciseId;
  let ansArr = ["A", "B", "C", "D"];
  $("#collectContent").append(`
             <div class='colectChange${index}' style='display:none'>
             <h4>${index + 1}.${exeInfo.topics}</h4>
             <ul class="main2">
               <li>
                 <button class="btn btn-light">A</button>
                 <span>${exeInfo.options[0]}</span>
               </li>
               <li>
                 <button class="btn btn-light">B</button>
                 <span>${exeInfo.options[1]}</span>
               </li>
               <li>
                 <button class="btn btn-light">C</button>
                 <span>${exeInfo.options[2]}</span>
               </li>
               <li>
                 <button class="btn btn-light">D</button>
                 <span>${exeInfo.options[3]}</span>
               </li>
             </ul>
             <div class="button-box">
                      <button class="btn btn btn-primary deleteCollect" data-deletecollect='${
                        v._id
                      }'>取消收藏</button>
                      <div>
                        <button class="btn btn btn-primary preCol">上一题</button>
                        <button class="btn btn btn-primary  nextCol">下一题</button>
                      </div>
                    </div>
             <div class="footer">
               <h6>正确答案${ansArr[exeInfo.answer]}</h6>
               <p>解析：${exeInfo.analysis}</p>
             </div>
             </div>
  `);
}
